from django import forms
from django.contrib import admin
from django.http import JsonResponse
from import_export.admin import ExportActionModelAdmin
from simplepro.action import CellAction

from .models import Store


# Register your models here.

class StoreFilter(admin.SimpleListFilter):
    title = '店铺状态'
    parameter_name = 'status'

    def lookups(self, request, model_admin):
        if request.user.is_superuser:
            store_list = Store.objects.values_list('status', flat=True).distinct()
        else:
            store_list = Store.objects.filter(user=request.user).values_list('status', flat=True).distinct()
        return [(store, store) for store in store_list]

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(status=self.value())
        else:
            return queryset


class StoreForm(forms.ModelForm):
    class Meta:
        model = Store
        exclude = ('user','status','score')


@admin.register(Store)
class StoreAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'address', 'score','get_status')
    list_filter = (StoreFilter,)
    search_fields = ('name',)
    form = StoreForm
    actions = ('application_action', 'pass_action','fail_action')

    # 显示添加
    def get_list_display(self, request):
        if not request.user.is_superuser:
            return self.list_display
        else:
            return ('name', 'description', 'address', 'score','pass_status')

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=request.user)

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super().save_model(request, obj, form, change)

    def application_action(self, request, queryset):

        obj = queryset.first()
        obj.status = '申请中'
        obj.save()
        return JsonResponse(data={
            'status': 'success',
            'msg': '正在申请，请耐心等候！'
        })

    # 可以添加一个确认提示
    application_action.confirm = "您确定要执行申请吗？"
    application_action.short_description= "申请"

    @admin.display(description='营业状态')
    def get_status(self, obj):
        if obj.status == '申请':
            return CellAction(text='<el-button type="success" round>申请</el-button>', action=self.application_action)
        else:
            return obj.status

    def pass_action(self, request, queryset):
        obj = queryset.first()
        obj.status = '营业中'
        obj.save()
        return JsonResponse(data={
            'status': 'success',
            'msg': '已通过申请！'
        })
    pass_action.short_description = "通过"
    def fail_action(self, request, queryset):
        obj = queryset.first()
        obj.status = '申请'
        obj.save()
        return JsonResponse(data={
            'status': 'success',
            'msg': '封禁成功！'
        })
    fail_action.short_description = "封禁"
    fail_action.confirm="您确定要执行封禁吗？"
    @admin.display(description='审核')
    def pass_status(self, obj):
        if obj.status == '申请中':
            return CellAction(text='<el-button type="success" round>通过</el-button>', action=self.pass_action)
        elif obj.status == '营业中':
            return CellAction(text='<el-button type="warning" round>营业中</el-button>', action=self.fail_action)
        else:
            return obj.status

    pass_action.confirm = "您确定要执行通过吗？"
